﻿#include "phnt/phnt_windows.h"
#include "phnt/phnt.h"
#include <iostream>

void printAllFunctionName(HANDLE hModule){

    if (hModule == NULL) {
        std::cerr << "Failed to load DLL." << std::endl;
        return ;
    }

    // 获取PE头指针
    PIMAGE_DOS_HEADER pDOSHeader = (PIMAGE_DOS_HEADER)hModule;
    PIMAGE_NT_HEADERS pNTHeaders = (PIMAGE_NT_HEADERS)((BYTE*)hModule + pDOSHeader->e_lfanew);

    // 获取导出表的地址
    DWORD exportDirRVA = pNTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
    PIMAGE_EXPORT_DIRECTORY pExportDir = (PIMAGE_EXPORT_DIRECTORY)((BYTE*)hModule + exportDirRVA);

    // 获取导出表的信息
    DWORD* pAddressOfFunctions = (DWORD*)((BYTE*)hModule + pExportDir->AddressOfFunctions);
    DWORD* pAddressOfNames = (DWORD*)((BYTE*)hModule + pExportDir->AddressOfNames);
    WORD* pAddressOfNameOrdinals = (WORD*)((BYTE*)hModule + pExportDir->AddressOfNameOrdinals);

    // 遍历所有导出函数
    for (DWORD i = 0; i < pExportDir->NumberOfNames; i++) {
        char* functionName = (char*)((BYTE*)hModule + pAddressOfNames[i]);
        DWORD functionRVA = pAddressOfFunctions[pAddressOfNameOrdinals[i]];
        void* functionAddress = (BYTE*)hModule + functionRVA;

            std::cout << "Function Name: " << functionName << std::endl;
            std::cout << "Function Address: " << functionAddress << std::endl;

    }
    return;
}


int main(){

    HANDLE hKernel32 = LoadLibraryA("kernel32.dll");
    printAllFunctionName(hKernel32);
    CloseHandle(hKernel32);

    return 0;
}
